package com.tida.servir.pages;

import com.tida.servir.entities.Evento;
import com.tida.servir.entities.Usuario;
import com.tida.servir.base.GeneralPage;
import com.tida.servir.entities.*;
import helpers.*;
import java.io.*;
import java.util.*;
import java.util.List;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import org.apache.tapestry5.Asset;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.services.Cookies;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.util.TextStreamResponse;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author LFL
 */
public class ImprimirSancion extends GeneralPage {

    @PageActivationContext
    private LkBusquedaSancion sancion;
    @Inject
    private Cookies galleta;
    @Inject
    @Path(value = "context:WEB-INF/reports/RegistroSancion.jrxml")
    private Asset RegistroSancion;

    @Log
    StreamResponse onActivate() throws IOException, JRException {
        //System.out.println(galleta.readCookieValue("_67646d8fee24c82ba512c519c16b2302"));
        //Valida si viene del enlace de busqueda
        if (sancion != null) {
            if (galleta.readCookieValue("_67646d8fee24c82ba512c519c16b2302").equals(Encriptacion.MD5(String.valueOf(sancion.getId_sancion()))) == false) {
                System.out.println("Sin acceso");
                String text = "<html><body>Por motivos de seguridad, usted no tiene acceso a la p&aacute;gina.</body></html>";
                return new TextStreamResponse("text/html", text);
            } else {
                Encriptacion encripta = new Encriptacion();
                InputStream is = RegistroSancion.getResource().openStream();
                ArrayList<LkBusquedaSancion> listaSancion = new ArrayList<LkBusquedaSancion>();
                listaSancion.add(sancion);
                JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(listaSancion);

                Map reportParams = new HashMap();

                JasperDesign reportDesign = JRXmlLoader.load(is);
                JasperReport reportCompiled = JasperCompileManager.compileReport(reportDesign);
                JasperPrint reportPrinted = JasperFillManager.fillReport(reportCompiled, reportParams, ds);

                ByteArrayInputStream bais = new ByteArrayInputStream(JasperExportManager.exportReportToPdf(reportPrinted));

                return new PDFStreamResponse(bais, encripta.MD5("ReporteServir" + sancion.getId_sancion()));
            }
        }
        return null;
    }

    @Log
    StreamResponse retornaSancion() throws IOException, JRException {
        Encriptacion encripta = new Encriptacion();
        InputStream is = RegistroSancion.getResource().openStream();
        ArrayList<LkBusquedaSancion> listaSancion = new ArrayList<LkBusquedaSancion>();
        listaSancion.add(sancion);
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(listaSancion);

        Map reportParams = new HashMap();

        JasperDesign reportDesign = JRXmlLoader.load(is);
        JasperReport reportCompiled = JasperCompileManager.compileReport(reportDesign);
        JasperPrint reportPrinted = JasperFillManager.fillReport(reportCompiled, reportParams, ds);

        ByteArrayInputStream bais = new ByteArrayInputStream(JasperExportManager.exportReportToPdf(reportPrinted));

        return new PDFStreamResponse(bais, encripta.MD5("ReporteServir" + sancion.getId_sancion()));
    }

    @Log
    public void setModificasancion(LkBusquedaSancion imprimesancion) {
        sancion = imprimesancion;
    }
}